-- https://github.com/nvim-telescope/telescope.nvim

local utils = require("utils")

local pack = {}

pack.lazy = {
    "telescope.nvim",
    -- 使用动态路径查找telescope.nvim插件
    dir = (function()
        -- 获取根目录路径
        local home_dir = os.getenv("HOME") or os.getenv("USERPROFILE")
        if not home_dir then
            print("错误: 无法获取HOME环境变量")
            return ""
        end
        
        -- 规范化路径分隔符 - 使用正斜杠以兼容SH终端（基于BUSYBOX）
        local function normalize_path(path)
            return path:gsub("\\", "/")
        end
        
        -- 设置根目录
        local root_dir = normalize_path(home_dir)
        
        -- 从根目录构建telescope.nvim插件路径（直接从files开始）
                return utils.path.join(root_dir, "files", "self", "telescope.nvim")
    end)(),
    dependencies = {
        { "nvim-lua/plenary.nvim" },
        {
            "telescope-fzf-native.nvim",
            -- 使用动态路径查找telescope-fzf-native.nvim插件
            dir = (function()
                -- 获取根目录路径
                local home_dir = os.getenv("HOME") or os.getenv("USERPROFILE")
                if not home_dir then
                    print("错误: 无法获取HOME环境变量")
                    return ""
                end
                
                -- 规范化路径分隔符 - 使用正斜杠以兼容SH终端（基于BUSYBOX）
                local function normalize_path(path)
                    return path:gsub("\\", "/")
                end
                
                -- 设置根目录
                local root_dir = normalize_path(home_dir)
                
                -- 从根目录构建telescope-fzf-native.nvim插件路径（直接从files开始）
                return utils.path.join(root_dir, "files", "self", "telescope-fzf-native.nvim")
            end)(),
            build = "cmake -S. -Bbuild -DCMAKE_BUILD_TYPE=Release && cmake --build build --config Release && cmake --install build --prefix build",
        },
    },
    lazy = true,
}

function pack.before_load()
    -- 不在这里 require telescope，因为插件还没有安装
end

function pack.load()
    -- 在这里 require telescope，因为此时插件已经由 lazy.nvim 安装
    local telescope = require("telescope")
    
    telescope.setup({
        defaults = {
            prompt_prefix = "・ ",
            selection_caret = "・ ",
            entry_prefix = " ",
            multi_icon = "・ ",
            color_devicons = true,
            file_ignore_patterns = { "node_modules" },
            -- theme
            layout_strategy = "bottom_pane",
            -- config
            layout_config = {
                bottom_pane = {
                    height = 15,
                    preview_cutoff = 100,
                    prompt_position = "bottom",
                },
            },
        },
        pickers = {
            diagnostics = {
                mappings = {
                    i = {
                        ["<c-s>"] = "file_split",
                        ["<c-q>"] = "close",
                    },
                    n = {
                        ["<c-s>"] = "file_split",
                        ["<c-q>"] = "close",
                    },
                },
            },
            find_files = {
                mappings = {
                    i = {
                        ["<c-s>"] = "file_split",
                        ["<c-q>"] = "close",
                    },
                    n = {
                        ["<c-s>"] = "file_split",
                        ["<c-q>"] = "close",
                    },
                },
            },
            live_grep = {
                mappings = {
                    i = {
                        ["<c-s>"] = "file_split",
                        ["<c-q>"] = "close",
                    },
                    n = {
                        ["<c-s>"] = "file_split",
                        ["<c-q>"] = "close",
                    },
                },
            },
            buffers = {
                mappings = {
                    i = {
                        ["<c-d>"] = "delete_buffer",
                        ["<c-q>"] = "close",
                    },
                    n = {
                        ["dd"] = "delete_buffer",
                        ["<c-q>"] = "close",
                    },
                },
            },
        },
        extensions = {
            fzf = {
                fuzzy = true,
                override_generic_sorter = true,
                override_file_sorter = true,
                case_mode = "smart_case",
            },
        },
    })
end

function pack.after_load()
    -- 确保使用完整的字符串，避免被意外截断
    local extension_name = string.format("%s", "fzf")
    require("telescope").load_extension(extension_name)
end

function pack.register_maps()
    utils.map.bulk_register({
        {
            mode = { "n" },
            lhs = "<leader>ff",
            rhs = function()
                require("telescope.builtin").find_files()
            end,
            options = { silent = true },
            description = "Find files in the current workspace",
        },
        {
            mode = { "n" },
            lhs = "<leader>fg",
            rhs = function()
                require("telescope.builtin").live_grep()
            end,
            options = { silent = true },
            description = "Find string in the current workspace",
        },
        {
            mode = { "n" },
            lhs = "<leader>fo",
            rhs = function()
                require("telescope.builtin").oldfiles()
            end,
            options = { silent = true },
            description = "Find telescope history",
        },
        {
            mode = { "n" },
            lhs = "<leader>fl",
            rhs = function()
                require("telescope.builtin").resume()
            end,
            options = { silent = true },
            description = "Find last lookup",
        },
        {
            mode = { "n" },
            lhs = "<leader>fh",
            rhs = function()
                require("telescope.builtin").help_tags()
            end,
            options = { silent = true },
            description = "Find all help document tags",
        },
        {
            mode = { "n" },
            lhs = "<leader>fm",
            rhs = function()
                require("telescope.builtin").marks()
            end,
            options = { silent = true },
            description = "Find marks in the current workspace",
        },
        {
            mode = { "n" },
            lhs = "<leader>fi",
            rhs = function()
                require("telescope.builtin").highlights()
            end,
            options = { silent = true },
            description = "Find all neovim highlights",
        },
        {
            mode = { "n" },
            lhs = "<leader>fb",
            rhs = function()
                require("telescope.builtin").buffers()
            end,
            options = { silent = true },
            description = "Find all buffers",
        },
        {
            mode = { "n" },
            lhs = "<leader>f/",
            rhs = function()
                require("telescope.builtin").search_history()
            end,
            options = { silent = true },
            description = "Find all search history",
        },
        {
            mode = { "n" },
            lhs = "<leader>f:",
            rhs = function()
                require("telescope.builtin").command_history()
            end,
            options = { silent = true },
            description = "Find all command history",
        },
    })
end

return pack